home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / postings < prev    next >
Encoding:
AWK Script  |  1997-08-26  |  4.6 KB  |  168 lines

  1. #!/usr/local/bin/gawk -f
  2. #!/usr/bin/awk -f
  3. # @(#) postings.gawk 1.0 94/03/09
  4. # 93/01/24 john h. dubois iii (john@armory.com)
  5. # 93/07/02 Added help, fixed Total.
  6. # 94/03/09 Use gawk so - options can be given
  7. BEGIN {
  8.     Usage = "Usage: postings [-h] article-file ..."
  9.     if (ARGC < 2) {
  10.     print Usage
  11.     print "Use -h for help."
  12.     exit(1)
  13.     }
  14.     if (ARGV[1] ~ "^[-+]h$") {
  15.     print \
  16. "postings: report number of postings to groups.\n"\
  17. Usage "\n"\
  18. "article-file is a file that postings are saved to, in mailbox format."
  19.     exit(0)
  20.     }
  21.     GoodFile = Total = 0
  22.     for (FileNum = 1; FileNum < ARGC; FileNum++) {
  23.     InFile = ARGV[FileNum]
  24.     while ((ret = (getline < InFile)) == 1) {
  25.         if ($1 == "\001\001\001\001") {
  26.         InHeader = 1
  27.         }
  28.         else if ($0 == "")
  29.         InHeader = 0
  30.         else if (InHeader && $1 == "Newsgroups:") {
  31.         Total++
  32.         split($2,Groups,",")
  33.         for (i = 1; i in Groups; i++)
  34.             GroupCt[Groups[i]]++
  35.         }
  36.     }
  37.     close(InFile)
  38.     if (ret)
  39.         printf "Error reading file %s.\n",InFile
  40.     else
  41.         GoodFile = 1
  42.     }
  43.     if (!GoodFile)
  44.     exit(1)
  45.     NGroup = qsortByArbIndex(GroupCt,k)
  46.     for (i = 1; i <= NGroup; i++) {
  47.     group = k[i]
  48.     printf "%3d %s\n",GroupCt[group],group
  49.     }
  50.     printf "%3d Total\n",Total
  51. }
  52.  
  53. ### Begin qsort routines
  54.  
  55. # Arr[] is an array of values with arbitrary indices.
  56. # k[] is returned with numeric indices 1..n.
  57. # The values in k[] are the indices of Arr[],
  58. # ordered so that if Arr[] is stepped through
  59. # in the order Arr[k[1]] .. Arr[k[n]], it will be stepped
  60. # through in order of the values of its elements.
  61. # The return value is the number of elements in the arrays (n).
  62. function qsortArbIndByValue(Arr,k,  ArrInd,ElNum) {
  63.     ElNum = 0
  64.     for (ArrInd in Arr)
  65.     k[++ElNum] = ArrInd
  66.     qsortSegment(Arr,k,1,ElNum)
  67.     return ElNum
  68. }
  69.  
  70. # Sort a segment of an array.
  71. # Arr[] contains data with arbitrary indices.
  72. # k[] has indices 1..nelem, with the indices of arr[] as values.
  73. # This function sorts the elements of arr that are pointed to by
  74. # k[start..end], swapping the values of elements of k[] so that
  75. # when this function returns arr[k[start..end]] will be in order.
  76. function qsortSegment(Arr,k,start,end,  left,right,sepval,tmp,tmpe,tmps) {
  77.     # handle two-element case explicitly for a tiny speedup
  78.     if ((end - start) == 1) {
  79.     if (Arr[tmps = k[start]] > Arr[tmpe = k[end]]) {
  80.         k[start] = tmpe
  81.         k[end] = tmps
  82.     }
  83.     return
  84.     }
  85.     # Make sure comparisons act on these as numbers
  86.     left = start+0
  87.     right = end+0
  88.     sepval = Arr[k[int((left + right) / 2)]]
  89.     # Make every element <= sepval be to the left of every element > sepval
  90.     while (left < right) {
  91.     while (Arr[k[left]] < sepval)
  92.         left++
  93.     while (Arr[k[right]] > sepval)
  94.         right--
  95.     if (left < right) {
  96.         tmp = k[left]
  97.         k[left++] = k[right]
  98.         k[right--] = tmp
  99.     }
  100.     }
  101.     if (left == right)
  102.     if (Arr[k[left]] < sepval)
  103.         left++
  104.     else
  105.         right--
  106.     if (start < right)
  107.     qsortSegment(Arr,k,start,right)
  108.     if (left < end)
  109.     qsortSegment(Arr,k,left,end)
  110. }
  111.  
  112. # Arr[] is an array of values with arbitrary indices.
  113. # k[] is returned with numeric indices 1..n.
  114. # The values in k are the indices of Arr[],
  115. # ordered so that if Arr[] is stepped through
  116. # in the order Arr[k[1]] .. Arr[k[n]], it will be stepped
  117. # through in order of the values of its indices.
  118. # The return value is the number of elements in the arrays (n).
  119. # If the indexes are numeric, Numeric should be true, so that they can be
  120. # compared as such rather than as strings.  Numeric indexes do not have to be
  121. # contiguous.
  122. function qsortByArbIndex(Arr,k,Numeric,  ArrInd,ElNum) {
  123.     ElNum = 0
  124.     if (Numeric)
  125.     # Indexes do not preserve numeric type, so must be forced
  126.     for (ArrInd in Arr)
  127.         k[++ElNum] = ArrInd+0
  128.     else
  129.     for (ArrInd in Arr)
  130.         k[++ElNum] = ArrInd
  131.     qsortNumIndByValue(k,1,ElNum)
  132.     return ElNum
  133. }
  134.  
  135. # Arr is an array of elements with contiguous numeric indexes to be sorted
  136. # by value.
  137. # start and end are the starting and ending indexes of the range to be sorted.
  138. function qsortNumIndByValue(Arr,start,end,  left,right,sepval,tmp,tmpe,tmps) {
  139.     # handle two-element case explicitly for a tiny speedup
  140.     if ((start - end) == 1) {
  141.     if ((tmps = Arr[start]) > (tmpe = Arr[end])) {
  142.         Arr[start] = tmpe
  143.         Arr[end] = tmps
  144.     }
  145.     return
  146.     }
  147.     left = start+0
  148.     right = end+0
  149.     sepval = Arr[int((left + right) / 2)]
  150.     while (left < right) {
  151.     while (Arr[left] < sepval)
  152.         left++
  153.     while (Arr[right] > sepval)
  154.         right--
  155.     if (left <= right) {
  156.         tmp = Arr[left]
  157.         Arr[left++] = Arr[right]
  158.         Arr[right--] = tmp
  159.     }
  160.     }
  161.     if (start < right)
  162.     qsortNumIndByValue(Arr,start,right)
  163.     if (left < end)
  164.     qsortNumIndByValue(Arr,left,end)
  165. }
  166.  
  167. ### End qsort routines
  168.